#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"StdThread.h"
#include"ThreadPool.h"
#include"StdTcp.h"


struct Message
{
    int flag;
    char fromName[20];
    char toName[20];
    char content[1024];
};
typedef struct Message Msg;

void *thread_handle(void *arg)
{
    TcpC *c =(TcpC *)arg;
    while(1)
    {
        Msg m;
        TcpClientRecv(c,&m,sizeof(m));
        printf("recv message from [%s],message : %s\n",m.fromName,m.content);
    }
}

int main(int argc,void *argv[])
{
    if(argc!=3)
    {
        printf("invalid numbers!\n");
        return -1;
    }
    TcpC *c=InitTcpClient(argv[1],atoi(argv[2]));

    Msg m;      //先给服务器发一次消息告诉服务器我是谁
    scanf("%s",m.fromName);
    m.flag=1;
    TcpClientSend(c,&m,sizeof(m));
    m.flag=2;

    thread *t=InitThread(thread_handle,c);
    DetachThread(t);

    while(1)
    {
        printf("please input send name : ");
        scanf("%s",m.toName);
        while(getchar()!='\n');     //scanf后就加一个getchar防止输入出问题
        printf("please input send content : ");
        scanf("%[^\n]",m.content);
        while(getchar()!='\n');
        TcpClientSend(c,&m,sizeof(m));
    }
    return 0;
}

